//入口
//grunt registerTask 注册任务
const sass = require('sass')
const loadGruntTasks = require('load-grunt-tasks')

module.exports = grunt => {
    //插件的使用
    // grunt.loadNpmTasks('grunt-contrib-clean')
    //使用grunt-sass
    // grunt.loadNpmTasks('grunt-sass')
    loadGruntTasks(grunt) //自动加载所有的grunt 插件


    grunt.initConfig({
        foo: {
            bar: 123
        },
        build: {
            //作为任务的配置选项
            options: {
                foo: 'bar'
            },
            css: '1',
            js: '2'
        },
        clean: {
            temp: 'temp/**'
        },
        sass: {
            options: {
                sourceMap: true,
                implementation: sass
            },
            main: {
                files: {
                    'dist/css/main.css': 'src/scss/*.scss'
                }
            }
        },
        babel: {
            options: {
                sourceMap: true,
                presets: ['@babel/preset-env']
            },
            main: {
                files: {
                    'dist/js/app.js': 'src/js/app.js'
                }
            }
        },
        watch: {
            js: {
                files: ['src/js/*.js'],
                tasks: ['babel']
            },
            css: {
                files: ['src/scss/*.scss'],
                tasks: ['sass']
            }
        }
    })


    grunt.registerTask('foo', () => {
        console.log(grunt.config('foo.bar'))
        console.log('hello Grunt')
    })

    grunt.registerTask('bar', '任务描述', () => {
        console.log('bar task~~')
    })
    //默认任务
    // grunt.registerTask('default', 'this is default task', () => {
    //     console.log('default task~')
    // })


    grunt.registerTask('async-task', function () {
        const done = this.async()
        setTimeout(() => {
            console.log('async task~~~')
            done()
        }, 1000);
    })

    grunt.registerTask('bad', () => {
        return false
        console.log('bad task~~')
    })

    grunt.registerTask('async-bad-task', function () {
        const done = this.async()
        setTimeout(() => {
            done(false)
            console.log('async bad task~~')
        }, 1000);
    })

    //多目标任务
    grunt.registerMultiTask('build', function () {
        //this.options 当前任务的配置选项
        console.log(this.options())

        //this.target 当前目标名称 this.data 当前目标的值
        console.log(`target: ${this.target}, data: ${this.data}`)
    })


    grunt.registerTask('default', ['sass', 'babel', 'watch'])
    


}